package com.chenzehe.zdmya.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenzehe.java.core.orm.Page;
import com.chenzehe.java.core.orm.PageRequest;
import com.chenzehe.zdmya.dao.CollectionDao;
import com.chenzehe.zdmya.domain.Collection;

@Service
@Transactional
public class CollectionService extends BaseService implements
		ICollectionService {
	@Resource
	CollectionDao collectionDao;
	@Resource
	IContentService contentService;

	@Resource
	IFxService fxService;

	@Resource
	IJyService jyService;

	@Override
	public void save(Collection collection) {
		collectionDao.save(collection);
		if (collection.getType() == Collection.CollectionType.CONTENT.type()) {
			contentService.updateCollectionCount(collection.getContentId());
		} else if (collection.getType() == Collection.CollectionType.FX.type()) {
			fxService.updateCollectionCount(collection.getContentId());
		} else if (collection.getType() == Collection.CollectionType.JY.type()) {
			jyService.updateCollectionCount(collection.getContentId());
		}
	}

	@Override
	public Page<Collection> queryByUserAndType(PageRequest pageRequest,
			int userId, int type) {
		StringBuilder sql = new StringBuilder(
				"SELECT o.title,o.img,c.content_id,c.id,c.issue_time,c.type FROM tbl_collection c LEFT JOIN ");
		if (type == Collection.CollectionType.CONTENT.type()) {
			sql.append("tbl_content o");
		} else if (type == Collection.CollectionType.FX.type()) {
			sql.append("tbl_fx o");
		} else if (type == Collection.CollectionType.JY.type()) {
			sql.append("tbl_jy o");
		}
		sql.append(" ON c.content_id=o.id WHERE c.user_id=? AND c.type=? ORDER BY c.id DESC LIMIT ?,?");
		Object[] paramsObjects = new Object[] { userId, type,
				(pageRequest.getPageNo() - 1) * pageRequest.getPageSize(),
				pageRequest.getPageSize() };
		List<Collection> listCollection = jdbcTemplate.query(sql.toString(),
				paramsObjects, new RowMapper<Collection>() {
					public Collection mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						return initCollection(rs);
					}
				});
		Page<Collection> pageCollection = new Page<Collection>(pageRequest);
		String countSql = "SELECT COUNT(*) FROM tbl_collection WHERE user_id=? AND type=?";
		Long count = jdbcTemplate.queryForLong(countSql, userId, type);
		pageCollection.setTotalItems(count);
		pageCollection.setResult(listCollection);

		return pageCollection;
	}

	private Collection initCollection(ResultSet rs) throws SQLException {
		Collection collection = new Collection();
		collection.setId(rs.getInt("id"));
		collection.setContentId(rs.getInt("content_id"));
		collection.setTitle(rs.getString("title"));
		collection.setImg(rs.getString("img"));
		collection.setIssueTime(rs.getTimestamp("issue_time"));
		collection.setType(rs.getInt("type"));
		return collection;
	}

	@Override
	public Collection queryByUserAndCollectionId(int userId, int coteneId,
			int type) {
		String hql = "FROM Collection WHERE userId=? AND contentId=? AND type=?";
		return collectionDao.findUnique(hql, userId, coteneId, type);
	}

	@Override
	public Long queryCollectionNum(int userId) {
		String sql = "SELECT COUNT(*) FROM tbl_collection WHERE user_id=?";
		return jdbcTemplate.queryForLong(sql, userId);
	}

	@Override
	public void deleteByIdAndUser(int id, int userId) {
		String hql = "DELETE FROM Collection WHERE id=? AND userId=?";
		collectionDao.batchExecute(hql, id, userId);
	}
}
